/*
@author Anpt
MeasureArea 测面积
*/
define(["dojo/_base/declare"], function (declare) {
    var instance = declare(null, {});
    //订阅消息
    instance.action = function (map){
	require(["esri/Color","esri/layers/GraphicsLayer", "esri/geometry/webMercatorUtils","esri/geometry/geodesicUtils","esri/graphic", "esri/geometry/Point", "esri/geometry/Polyline","esri/geometry/Polygon","esri/toolbars/draw", "dojo/on","esri/symbols/TextSymbol","esri/symbols/SimpleMarkerSymbol","esri/symbols/SimpleLineSymbol",
	         "esri/symbols/SimpleFillSymbol", "esri/symbols/PictureMarkerSymbol","esri/geometry/mathUtils"], function ( Color,GraphicsLayer,webMercatorUtils,geodesicUtils,Graphic, Point, Polyline,Polygon,Draw, on,TextSymbol,MarkerSymbol,SimpleLineSymbol,SimpleFillSymbol,PictureMarkerSymbol,mathUtils) {
	    var suijishu=new Date().getTime();
	    var resultPloygon=null;
	    var isfinish = 0;
		var meatureAreaLayer = new GraphicsLayer();
		//存储所有graphic图层
		ioc.map.graphiclayers.push(meatureAreaLayer);
		var sfs =new SimpleFillSymbol().setColor(null).outline.setColor("red");
		var drowBar=new  Draw(map);
		drowBar.activate(Draw["POLYGON"]);
		meatureAreaLayer.on("click", function (evt) {
			 console.log(meatureAreaLayer.graphics);
       	 for( var i=meatureAreaLayer.graphics.length - 1;i>=0;i--){
       		 debugger;
           	 if (evt.graphic.bType ==meatureAreaLayer.graphics[i].bType) {
           		 meatureAreaLayer.remove(meatureAreaLayer.graphics[i]);
                }
           }
        });
	    drowBar.on("draw-end",function(evt){
	    	drowBar.deactivate();
	    	isfinish=1;
	    	var resultPloygon=evt.geometry;
			var resultGraphic=new Graphic(resultPloygon,sfs);
			resultGraphic.bType = "MeasureArea"+suijishu;
			var geo=webMercatorUtils.webMercatorToGeographic(resultPloygon);
			var area=Math.abs(geodesicUtils.geodesicAreas([geo], esri.Units.SQUARE_METERS)).toFixed(2);
			meatureAreaLayer.add(resultGraphic);
			drawfinsh(resultPloygon,area);
			
		});
		function drawfinsh(resultPloygon,area){
			var areatext="";
			if(area<667){
				areatext="总面积："+area+"平方米";
			}else{
				areatext="总面积："+(area/667.67).toFixed(3)+"亩";
			}
			
			if(isfinish==1){
				var closeSymbol = new PictureMarkerSymbol({
					"url": "gistech/widgets/ManageTools/images/measure_close.png",
                    "height": 12,
                    "width": 12,
                    "type": "esriPMS",
                    "xoffset": 3,
                    "yoffset": -10
	             });
				 var centerOfPolygon=resultPloygon.getCentroid();
				 
				 var closeGraphic = new Graphic(centerOfPolygon, closeSymbol);
				 console.log(centerOfPolygon)
				 
				 closeGraphic.bType = "MeasureArea"+suijishu;
				 console.log(closeGraphic);
				 meatureAreaLayer.add(closeGraphic);
				 addResult(resultPloygon,areatext);
			}
			resultPloygon=null;
		}
		function strLength(str) {
	        var realLength = 0, len = str.length, charCode = -1;
	        for (var i = 0; i < len; i++) {
	            charCode = str.charCodeAt(i);
	            if (charCode >= 0 && charCode <= 128) realLength += 1;
	            else realLength += 2;
	        }
	        return realLength;
	    }
		function addResult(resultPloygon,text){
			 require(["esri/symbols/TextSymbol", "esri/symbols/PictureMarkerSymbol"], function (TextSymbol, PictureMarkerSymbol) {
	                //文本符号graphic对象
				    var len = strLength(text) * 5+20;
				    console.log(len);
	                var symbol = new TextSymbol(text).setColor(new Color([255, 0, 0]));//文本的符号
	                symbol.setOffset(len / 2 + 30, -5); //符号的偏移
	                var graphic = new Graphic(resultPloygon, symbol);
	                graphic.bType = "MeasureArea"+suijishu;
	                meatureAreaLayer.add(graphic);
	            });
			
			
		}
	    map.addLayer(meatureAreaLayer);
	});
    }
    return instance;
});